package com.david.springcloud.filter;

import org.springframework.boot.web.servlet.server.Session;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;



/**
 * @Description: MyGatewayFilter
 * @Author: wangwei
 * @CreateDate: 2022/1/19 10:47
 */

//@Component
public class MyGatewayFilter implements GlobalFilter,Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) {

        ServerHttpRequest request=serverWebExchange.getRequest();

        if(request.getCookies()==null){
            System.err.println("**********非法用户*********");
        }else {
//            HttpCookie httpCookie=request.getCookies().getFirst("token");
//            String value = httpCookie.getValue();
//
//            System.err.println("**********value:>>"+value);
//            if (value == null) {
//                System.err.println("**********非法用户*********");
//                serverWebExchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
//                return serverWebExchange.getResponse().setComplete();
//            }
        }


        return gatewayFilterChain.filter(serverWebExchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
